﻿# coding: utf-8

from Command import CCommand

import Transformation
import SceneNode

class CCmdTranslation(CCommand):
    def __init__(self, **p):
        """
        Default class contructor.

        Parameters:
        @param nodeRef - the weak reference of the scene node
        @param newTranslation - the new translation of the scene node (x, y, z)
        """

        CCommand.__init__(self, TD.CMD_TRANSLATION, **p)

    def DoCmd(self):
        try:
            nodeRef = self._paramDict['nodeRef']
            newX, newY, newZ = self._paramDict['newTranslation']
            
            node = nodeRef()
            coreHandle = node.GetCoreHandle()

            # save the old translation
            self._x, self._y, self._z = Transformation.GetRelativeTranslation(SceneNode._CTransformation(coreHandle))
            Transformation.SetRelativeTranslation(SceneNode._CTransformation(coreHandle), newX, newY, newZ)
        
        except:
            SHOW_TRACE()

    def UndoCmd(self):
        try:
            nodeRef = self._paramDict['nodeRef']
            node = nodeRef()
            coreHandle = node.GetCoreHandle()

            # restore the old translation
            Transformation.SetRelativeTranslation(SceneNode._CTransformation(coreHandle), self._x, self._y, self._z)
            TRANSLATION_TOOL.ResetPosition()

        except:
            SHOW_TRACE()

    def UpdateTranslation(self, dx, dy, dz):
        try:
            nodeRef = self._paramDict['nodeRef']
            node = nodeRef()
            coreHandle = node.GetCoreHandle()

            # restore the old translation
            newX, newY, newZ = self._x + dx, self._y + dy, self._z + dz
            self._paramDict['newTranslation'] = (newX, newY, newZ)
            Transformation.SetRelativeTranslation(SceneNode._CTransformation(coreHandle), newX, newY, newZ)

        except:
            SHOW_TRACE()

        return True